<?php
//Include database connection page
include '..'.DIRECTORY_SEPARATOR.'db-connection'.DIRECTORY_SEPARATOR.'conn.php';
$requestedAction = $_REQUEST["action"];

if($requestedAction=="insertcategory")
{
	$categoryName = $_REQUEST["catname"];
	$categoryName = addslashes($categoryName);
	$sqlToCheckDuplicate = "SELECT COUNT(*) as duplicateCount FROM tbl_category_mst WHERE category_name='".$categoryName."'";
	$run = mysql_query($sqlToCheckDuplicate);
	if($getData = mysql_fetch_array($run))
	{
		$isDuplicate = $getData['duplicateCount'] ;
	}
	if($isDuplicate==0)
	{
		$sqlToInsert  = "INSERT INTO tbl_category_mst (category_name,is_active) VALUES ('$categoryName','Y')";
		$run = mysql_query($sqlToInsert);
		if($run)
			echo 1;
		else 
			echo 0;
	}
	else
	{
		echo 2;
	}
}
else if($requestedAction=="insertsubcategory")
{
	$categoryId = $_REQUEST["catid"];
	$SubcategoryName = $_REQUEST["subcatname"];
	$SubcategoryName = addslashes($SubcategoryName);
	
	//INSERT SUBCAT in DB
	$sqlToInsertSubCat = "INSERT INTO tbl_subcategory_mst (subcat_name,is_active) VALUES ('$SubcategoryName','Y')";
	$run1 = mysql_query($sqlToInsertSubCat);
	
	//GET SUBCAT ID FROM DB
	$sqlToGetSubCatId = "SELECT id FROM `tbl_subcategory_mst` WHERE subcat_name='$SubcategoryName' ORDER BY id DESC LIMIT 1";
	$dataRe = mysql_query($sqlToGetSubCatId);
	while ($data = mysql_fetch_array($dataRe))
	{
		$subCatId = $data['id'];
	}
	
	//INSERT INTO CAT SUBCAT MAP TABLE
	$sqlToInsertMap = "INSERT INTO tbl_category_subcategory_map (category_id,subcategory_id,is_active) VALUES ('$categoryId','$subCatId','Y')";
	$run1 = mysql_query($sqlToInsertMap);
	if($run1)
		echo 1;
	else
		echo 0;
}
else if($requestedAction=="deletecategory")
{
	$categoryId = $_REQUEST["catid"];
	if(strlen($categoryId)>0)
	{
		$sqlToupdate = "UPDATE tbl_category_mst SET is_active='N' WHERE id='".$categoryId."'";
		//file_put_contents("Log_test_db-insert.php", $sqlToupdate);
		$run = mysql_query($sqlToupdate);
		if($run)
		{
			$sqlUpdateSubCatInactive = "UPDATE tbl_subcategory_mst SET is_active='N' WHERE id IN (SELECT subcategory_id FROM tbl_category_subcategory_map WHERE category_id='$categoryId')";
			$update = mysql_query($sqlUpdateSubCatInactive);
			echo 1;
		}
	}	
}
else if($requestedAction=="update-category")
{
	$categoryName = addslashes($_REQUEST['categoryname']);
	$categoryId = $_REQUEST['catid'];
	
	$updateQuery = "UPDATE tbl_category_mst SET category_name='".$categoryName."' WHERE id=$categoryId";
	$run = mysql_query($updateQuery);
	if($run)
	{
		echo 1;
	}
	else
	{
		echo 0;
	}
}
else if($requestedAction=="act")
{
	$id = $_REQUEST['id'];
	$updateQuery = "UPDATE tbl_user_master SET yn_is_active='Y' WHERE int_user_id='$id'";
	file_put_contents("Log_test_db-insert.php", $updateQuery);
	
	$run = mysql_query($updateQuery);
	if($run)
	{
		echo 1;
	}
	else
	{
		echo 0;
	}
}
else if($requestedAction=="deact")
{
	$id = $_REQUEST['id'];
	$updateQuery = "UPDATE tbl_user_master SET yn_is_active='N' WHERE int_user_id='$id'";
	$run = mysql_query($updateQuery);
	if($run)
	{
		echo 1;
	}
	else
	{
		echo 0;
	}
}







